/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.JavaXML.DOMValidator;

import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;

/**
 *
 * @author thanhtq
 */
public class Validate {

    public boolean validate(Document document,String filePath) throws Exception
    {
        try {
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            // create an object of Source class
            Source schemaFile = new StreamSource(new File(filePath));
            // load a WXS schema, represented by a Schema instance
            Schema schema = factory.newSchema(schemaFile);
            Validator validator = null;
            // create a Validator instance, which can be used to validate an instance document
            validator = schema.newValidator();
            // validate the DOM tree
            validator.validate(new DOMSource(document));
            return true;
        } catch (Exception ex) {
            throw ex;
        }
    }
}
